package com.lightsh.utility;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.lightsh.common.PaintItem;
import com.lightsh.common.PaintStroke;

import android.content.Context;
import android.gesture.Gesture;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.util.Log;

public class Utility {
	
	private static final boolean BITMAP_RENDERING_ANTIALIAS = true;
    private static final boolean BITMAP_RENDERING_DITHER = true;
    private static final int BITMAP_RENDERING_WIDTH = 2;
	/**
	 * 将手势转换成bitmap
	 * @param gesture
	 * @param width			目标图片的宽度
	 * @param height		目标图片的高度
	 * @param color			比划的颜色值
	 * @param strokeWidth  比划的宽度
	 * @param scale			gesture实际大小与图片的比例
	 * @return
	 */
    public static Bitmap gestureToBitmap(Gesture gesture, int width, int height, int color, float strokeWidth, float scale){
    	final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap);

        final Paint paint = new Paint();
        paint.setAntiAlias(BITMAP_RENDERING_ANTIALIAS);
        paint.setDither(BITMAP_RENDERING_DITHER);
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(BITMAP_RENDERING_WIDTH);

        final Path path = gesture.toPath();
        // 这里不需要计算path的边框，因为path的边框就是用户可以绘制的区域
//        final RectF bounds = new RectF();
//        path.computeBounds(bounds, true);

        paint.setStrokeWidth(strokeWidth);
        
        // 这里的path不需要offset
//        path.offset(-bounds.left + (width - bounds.width() * scale) / 2.0f,
//                -bounds.top + (height - bounds.height() * scale) / 2.0f);

//        canvas.translate(inset, inset);
        
        canvas.scale(scale, scale);
        canvas.drawPath(path, paint);
        return bitmap;
    }
    
    public static byte[] bitmapToPNGBytes(Bitmap bitmap){
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){
			return out.toByteArray();
		}
		return null;
	}
    
    public static final String formatTime(long time){
    	Calendar now = Calendar.getInstance();
    	Calendar des = Calendar.getInstance();
    	des.setTimeInMillis(time);
    	
    	StringBuilder formatStr = new StringBuilder();
    	if(now.get(Calendar.YEAR) != des.get(Calendar.YEAR)){
    		formatStr.append("yyyy-");
    	}
    	if(now.get(Calendar.MONTH) == des.get(Calendar.MONTH) && now.get(Calendar.DAY_OF_MONTH) == des.get(Calendar.DAY_OF_MONTH)){
    		
    	}else{
    		if(des.get(Calendar.MONTH) >= 9){
        		formatStr.append("MM-");
        	}else{
        		formatStr.append("M-");
        	}
        	if(des.get(Calendar.DAY_OF_MONTH) >= 10){
        		formatStr.append("dd");
        	}else{
        		formatStr.append("d");
        	}
        	formatStr.append("    ");
    	}
    	formatStr.append("kk:mm");
    	
    	SimpleDateFormat formater = new SimpleDateFormat(formatStr.toString());
    	return formater.format(new Date(time));
    }
    
    public static final void pathQuadTo(Path inPath, float lastX, float lastY, float newX, float newY){
    	inPath.quadTo(lastX, lastY, (newX + lastX)/2, (newY + lastY)/2);
    }
    public static final void initDrawPaint(Paint paint){
    	paint.setAntiAlias(true);
		paint.setDither(true);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
    }
    
    public static void drawStroke(Path path, Paint paint, Canvas canvas, PaintStroke stroke){
		float[] points = stroke.getStrokePoints();
		int color = stroke.getColor();
		int width = stroke.getWidth();
		final int size = points.length;
		if(size > 0){
			path.reset();
			paint.setColor(color);
			paint.setStrokeWidth(width);
			path.moveTo(points[0], points[1]);
			float lastX = points[0];
			float lastY = points[1];
			for(int i = 2; i < size; i+=2){
				Utility.pathQuadTo(path, lastX, lastY, points[i], points[i + 1]);
				lastX = points[i];
				lastY = points[i + 1];
			}
			path.lineTo(lastX, lastY);
			canvas.drawPath(path, paint);
			path.reset();
		}
	}
    public static final Bitmap paintToBitmap(Context context, PaintItem item, int width, int height){
    	ArrayList<PaintStroke> list = item.getStrokeList(context);
    	Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		Path path = new Path();
    	Paint paint = new Paint();
    	initDrawPaint(paint);
    	
    	float originalWidth = item.getWidth();
    	float originalHeight = item.getHeight();
    	
    	float widthScale = width / originalWidth;
    	float heightScale = height / originalHeight;
    	float scale = Math.min(widthScale, heightScale);
    	if(widthScale < heightScale){
    		canvas.translate(0, (height - scale * originalHeight) / 2);
    	}else{
    		canvas.translate((width - scale * originalWidth) / 2, 0);
    	}
    	canvas.scale(scale, scale);
    	
    	for(PaintStroke stroke : list){
    		drawStroke(path, paint, canvas, stroke);
    	}
    	return bitmap;
    	
    }
}
